package app.utils;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import org.apache.commons.io.FileUtils;

public class ImageResizer {
	/***
	 * 功能 :调整图片大小 开发：luhox
	 * 
	 * 2013年8月15日 更新为File参数类型
	 * 
	 * 2017年2月5日，修改，释放占用的文件
	 * 
	 * @throws IOException
	 */
	public void resizeOnWH(File srcFile, File targetFile, Integer width,
			Integer height) throws IOException {

		Image srcImg = null;
		InputStream is = null;
		BufferedImage buffImg = null;
		try {
			srcImg = ImageIO.read(srcFile);
			is = new FileInputStream(srcFile);
			buffImg = ImageIO.read(is);

			if (buffImg.getWidth() > width || buffImg.getHeight() > height) {

				// System.out.println("图片调整");
				// 高度或者宽度超出
				buffImg = new BufferedImage(width, height,
						BufferedImage.TYPE_INT_RGB);
				buffImg.getGraphics().drawImage(
						srcImg.getScaledInstance(width, height,
								Image.SCALE_SMOOTH), 0, 0, null);

				ImageIO.write(buffImg, "JPEG", targetFile);
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			srcImg.flush();
			buffImg.flush();
			is.close();
		}
	}

	/***
	 * 按宽度调整
	 * 
	 * @param srcFile
	 * @param targetFile
	 * @param width
	 * @throws IOException
	 */
	public void resizeOnW(File srcFile, File targetFile, int width)
			throws IOException {
		//System.out.println("ImageResizer.resizeOnW()srcFile1="+srcFile);

		InputStream is = new FileInputStream(srcFile);
		BufferedImage buffImg = ImageIO.read(is);
		if (buffImg.getWidth() > width) {
			// 需要调整
			int height = buffImg.getHeight() * width / buffImg.getWidth();
			resizeOnWH(srcFile, targetFile, width, height);
		} else {
			// 宽度没有超出，不用调整
			FileUtils.copyFile(srcFile, targetFile);
		}
		is.close();
	}

	public void resizeOnW(String srcFile, String targetFile, int width)
			throws IOException {
		//System.out.println("ImageResizer.resizeOnW()srcFile=" + srcFile);
		//System.out.println("ImageResizer.resizeOnW()targetFile=" + targetFile);
		//System.out.println("ImageResizer.resizeOnW()width=" + width);

		File uploadedSrcFile = new File(srcFile);
		File uploadedFile = new File(targetFile);
		resizeOnW(uploadedSrcFile, uploadedFile, width);

	}

	/***
	 * 按高度调整
	 * 
	 * @param srcFile
	 * @param targetFile
	 * @param height
	 * @throws IOException
	 */
	public void resizeOnH(File srcFile, File targetFile, Integer height)
			throws IOException {

		BufferedImage buffImg = ImageIO.read(new FileInputStream(srcFile));
		if (buffImg.getHeight() > height) {
			// 需要调整
			int width = buffImg.getWidth() * height / buffImg.getHeight();
			resizeOnWH(srcFile, targetFile, width, height);
		} else {
			// 宽度没有超出，不用调整
			FileUtils.copyFile(srcFile, targetFile);
		}
	}

	public Integer getFileSize(File file) throws IOException {

		return (int) (file.length() / 1024);
	}

	public static void main(String[] args) throws IOException {
		String srcFile = "D:/data/projects/apache-tomcat-8/webapps/erms/uploadsrc/image/20170212/src1486911147709051151.jpg";
		String targetFile = "D:/data/projects/apache-tomcat-8/webapps/erms/upload/image/20170212/1486911147709051151.jpg";
		int width = 500;
		ImageResizer iResize = new ImageResizer();
		iResize.resizeOnW(srcFile, targetFile, width);
		
	}

}
